package com.zzx.bms.controller;

import com.zzx.bms.core.util.Page;
import com.zzx.bms.domain.*;
import com.zzx.bms.service.IDepartmentService;
import com.zzx.bms.service.INursestationService;
import com.zzx.bms.service.IRoleService;
import com.zzx.bms.service.IUserService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by SamZhao on 2016/12/6.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    private static Logger logger = Logger.getLogger(UserController.class);

    @Autowired
    private IUserService userService;

    @Autowired
    private IRoleService roleService;

    @Autowired
    private INursestationService nursestationService;

    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("/list")
    public String bookList(User user, Page page, Model model) throws Exception {

        // 取商品列表
        List<User> list = userService.findUserList(user);//查询

        List<Department> deplist = departmentService.findAllDepartmentList();
        List<Nursestation> nulist = nursestationService.findAllNursestationList();
        List<Role> rollist = roleService.findAllRoleList();

        List<User> resultList = createList(list,deplist,nulist,rollist);

        model.addAttribute("userList", resultList);
        return "user/userList";
    }

    private List<User> createList(List<User> list, List<Department> deplist, List<Nursestation> nulist, List<Role> rollist) {
        List<User> result = new ArrayList<User>();
        for(User temp : list){
            if(temp.getDepartmentId()!=null){
                for(Department deptemp : deplist){
                    if(deptemp.getId() == temp.getDepartmentId()){
                        temp.setDepartment(deptemp.getName());
                        break;
                    }
                }
            }
            if(temp.getNursestationId()!=null){
                for(Nursestation nutemp : nulist){
                    if(nutemp.getId()==(temp.getNursestationId())){
                        temp.setNursestation(nutemp.getName());
                        break;
                    }
                }
            }
            if(temp.getRoleId()!=null){
                for(Role deptemp : rollist){
                    if(deptemp.getId()==temp.getRoleId()){
                        temp.setRole(deptemp.getRoleMemo());
                        break;
                    }
                }
            }
            result.add(temp);
        }
        return result;
    }

    @RequestMapping(value = "/addUser")
    public String addUser(Model model){
        List<Department> deplist = departmentService.findAllDepartmentList();
        List<Nursestation> nulist = nursestationService.findAllNursestationList();
        List<Role> rollist = roleService.findAllRoleList();

        model.addAttribute("deplist", deplist);
        model.addAttribute("nulist", nulist);
        model.addAttribute("rollist", rollist);

        return "user/addUser";
    }

    @RequestMapping("/saveUser")
    public @ResponseBody String saveUser(User user) throws Exception {
        int flag = userService.saveUser(user);
        if(flag > 0)
            return "success";
        else
            return "fail";
    }

    @RequestMapping("/deleteUser")
    public @ResponseBody String deleteUser(User user, String id) throws Exception {
        int flag = userService.deleteUserById(id);
        if(flag > 0)
            return "success";
        else
            return "fail";
    }

    @RequestMapping(value = "/checkUser")
    public @ResponseBody String checkUser(Model model, User user){
        int flag = userService.checkUser(user);
        if(flag > 0)
            return "success";
        else
            return "fail";
    }

    @RequestMapping(value = "/checkUserByNum")
    public @ResponseBody String checkUserByNum(Model model, String number){
        User user = new User();
        user.setNumber(number);
        User user1 = userService.checkUserByNum(user);
        if(user1 != null )
            return user1.getId().toString();
        else
            return "null";
    }

    @RequestMapping(value = "/editUser")
    public String editUser(Model model, String id){
        User user = userService.findUserById(id);
        List<Department> deplist = departmentService.findAllDepartmentList();
        List<Nursestation> nulist = nursestationService.findAllNursestationList();
        List<Role> rollist = roleService.findAllRoleList();

        model.addAttribute("deplist", deplist);
        model.addAttribute("nulist", nulist);
        model.addAttribute("rollist", rollist);
        model.addAttribute("user", user);
        return "user/editUser";
    }

    @RequestMapping(value = "/showUser")
    public String showUser(Model model, String id){
        User user = userService.findUserById(id);
        List<Department> deplist = departmentService.findAllDepartmentList();
        List<Nursestation> nulist = nursestationService.findAllNursestationList();
        List<Role> rollist = roleService.findAllRoleList();

        model.addAttribute("deplist", deplist);
        model.addAttribute("nulist", nulist);
        model.addAttribute("rollist", rollist);
        model.addAttribute("user", user);
        return "user/showUser";
    }

}
